package com.sto.stoapp.activity.sales;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.util.UnifiedStaticType;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHhmEditText.EditClickListener;
import com.sto.stoapp.widget.StoHomeActivity;

/**
 * 订单自动下载
 * 
 * @author Administrator
 * 
 */
public class SalesOrderAutoDownTimerSetActivity extends StoHomeActivity {

	private StoHhmEditText setTimerEdit;
	private Button saveBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sales_order_down_timer_set);
		this.setTitleBottom(R.string.auto_down_timer,
				HomeBroadAction.SALES_MAN_BROAD);
		initWidget();
		initListener();
	}

	private void initWidget() {
		setTimerEdit = (StoHhmEditText) findViewById(R.id.set_timer_edit);
		setTimerEdit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		int time = StoFilePreference.getInstance(this).getAutoDownloadOrderTime();
		setTimerEdit.setHint(String.valueOf(time) + "(分)");
		saveBtn = (Button) findViewById(R.id.save);
	}

	private void initListener() {
		saveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				
				if (StoFilePreference.getInstance(SalesOrderAutoDownTimerSetActivity.this).getOrderFunctionControl()) {
					String setTimer = setTimerEdit.getText().toString();
					if (isDigit(setTimer)) {
						int time = Integer.valueOf(setTimer).intValue();
						if (time >= 5) {

							StoFilePreference.getInstance(
									SalesOrderAutoDownTimerSetActivity.this)
									.setAutoDownloadOrderTime(time);
							// 自动下载订单广播
							Intent Tintent = new Intent(UnifiedStaticType.TIMING_ACTION);
							Tintent.putExtra(UnifiedStaticType.TIMING_TYPE,
									UnifiedStaticType.AUTO_DOWNLOAD_ORDER_TIMING);
							sendBroadcast(Tintent);
							Helper.showToast(SalesOrderAutoDownTimerSetActivity.this, getText(R.string.save_success)
									.toString());
							finish();

						}else{
							Helper.showSoundToast(SalesOrderAutoDownTimerSetActivity.this, "订单下载时间必须大于等于5分钟!", false);
						}
					}else{
						Helper.showSoundToast(SalesOrderAutoDownTimerSetActivity.this, "请输入正确的时间!", false);
					}
				} else {
					Helper.showSoundToast(SalesOrderAutoDownTimerSetActivity.this, getText(R.string.order_function_no_open)
							.toString(), false);
				}	
			}
		});
		setTimerEdit.setEditDelete(new EditClickListener() {
            
            @Override
            public void onEditDelete() {
                onBackPressed();
            }
            
            @Override
            public void onEditClickListener(View v) {
                
            }
        });
	}
	

	
	@Override
	protected void onDestroy() {
		/*if (StoFilePreference.getInstance(this).getOrderFunctionControl()) {
			String setTimer = setTimerEdit.getText().toString();
			if (isDigit(setTimer)) {
				int time = Integer.valueOf(setTimer).intValue();
				if (time >= 1) {

					StoFilePreference.getInstance(
							SalesOrderAutoDownTimerSetActivity.this)
							.setAutoDownloadOrderTime(time);
					// 自动下载订单广播
					Intent Tintent = new Intent(UnifiedStaticType.TIMING_ACTION);
					Tintent.putExtra(UnifiedStaticType.TIMING_TYPE,
							UnifiedStaticType.AUTO_DOWNLOAD_ORDER_TIMING);
					sendBroadcast(Tintent);
					Helper.showToast(this, getText(R.string.save_success)
							.toString());

				}
			}
		} else {
			Helper.showToast(this, getText(R.string.order_function_no_open)
					.toString());
		}*/
		super.onDestroy();
	}
}
